/**
 *
 */
package org.svnadmin.service;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.svnadmin.tree.entity.Tree;
import org.svnadmin.tree.entity.TreeNode;
import org.svnadmin.tree.service.AbstractTreeNodeService;
import org.tmatesoft.svn.core.SVNDirEntry;
import org.tmatesoft.svn.core.SVNNodeKind;

import javax.annotation.Resource;
import java.util.*;

/**
 * 仓库目录结构树节点服务层
 *
 * @author <a href="mailto:yuanhuiwu@gmail.com">Huiwu Yuan</a>
 * @since 3.0.2
 */
@Service(RepTreeNodeService.BEAN_NAME)
public class RepTreeNodeService extends AbstractTreeNodeService {

    /**
     * Bean名称
     */
    public static final String BEAN_NAME = "repTreeNodeService";
    private static final String AND = "$AND$";
    /**
     * 日志
     */
    private final Logger LOG = Logger.getLogger(RepTreeNodeService.class);


    /**
     * 仓库服务层
     */
    @Resource(name = RepositoryService.BEAN_NAME)
    RepositoryService repositoryService;

    @Override
    public List<TreeNode> getTreeNodes(Tree parent,
                                       Map<String, Object> parameters) {
        List<TreeNode> results = new ArrayList<TreeNode>();

        String pj = (String) parameters.get("pj");
        String path = (String) parameters.get("path");
        path = StringUtils.replace(path, AND, "&");
        if (StringUtils.isBlank(pj)) {
            LOG.warn("pj id is blank ");
            return null;
        }
        try {
            Collection<SVNDirEntry> entries = this.repositoryService.getDir(pj, path);
            if (entries == null) {
                return null;
            }
//	    	 Collection<SVNDirEntry> entries = repository.getDir("/trunk", SVNRevision.HEAD.getNumber(), properties, (Collection) null);
            for (SVNDirEntry svnDirEntry : entries) {
//				System.out.println(svnDirEntry);
//	    		System.out.println(svnDirEntry.getName()//文件夹或文件名
//	    				+","+svnDirEntry.getKind()//类型,参考SVNNodeKind.FILE,SVNNodeKind.DIR
//	    				+","+svnDirEntry.getRevision()//版本
//	    				+","+svnDirEntry.getAuthor()//作者
//	    				+","+svnDirEntry.getSize()//如果kind是SVNNodeKind.FILE时返回文件的大小
//	    				+","+svnDirEntry.getDate());//日期
                TreeNode treeNode = new TreeNode(svnDirEntry.getName());
                treeNode.setLeaf(SVNNodeKind.FILE.equals(svnDirEntry.getKind()));//叶子?
                treeNode.addParamete("pj", pj);
                if (path.endsWith("/")) {
                    treeNode.addParamete("path", path + StringUtils.replace(svnDirEntry.getName(), "&", AND));
                } else {
                    treeNode.addParamete("path", path + "/" + StringUtils.replace(svnDirEntry.getName(), "&", AND));
                }
                results.add(treeNode);
            }
            Collections.sort(results);// 排序
        } catch (Exception e) {
            LOG.error(e.getMessage());

            results.clear();
            TreeNode errorNode = new TreeNode(e.getMessage());
            errorNode.setLeaf(true);
            results.add(errorNode);
            return results;
        }

        return results;
    }

}
